package cn.edu.cuc.yjsy.admin.util;

import org.eclipse.emf.ecore.EStructuralFeature;

import cn.edu.cuc.yjsy.model.ModelPackage;
import cn.edu.cuc.yjsy.model.Student;

/**
 * This class does the comparisons for sorting Player objects.
 */
public class StudentComparator implements ColumnComparator {

	/** Constant for ascending */
	public static final int ASCENDING = 0;

	/** Constant for descending */
	public static final int DESCENDING = 1;

	private int column;
	private int direction;
	
	public StudentComparator(){
		this.direction = ASCENDING;
	}

	/**
	 * Compares two Player objects
	 * 
	 * @param obj1 the first Student
	 * @param obj2 the second Student
	 * @return int
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object obj1, Object obj2) {
		int rc = 0;
		
		Student p1 = (Student) obj1;
		Student p2 = (Student) obj2;

		// Determine which field to sort on, then sort
		// on that field
		EStructuralFeature feature = 
			ModelPackage.eINSTANCE.getStudent().getEStructuralFeature(column);
		if(feature.getEType().getName().equals("EString")){
			if(p1.eGet(feature) != null	&& p2.eGet(feature) != null)
				rc = ((String)p1.eGet(feature)).compareTo((String)p2.eGet(feature));
			else if(p1.eGet(feature) != null)
				rc = 1;
			else if(p2.eGet(feature) != null)
				rc = -1;
		}

		// Check the direction for sort and flip the sign
		// if appropriate
		if (direction == DESCENDING) {
			rc = -rc;
		}
		return rc;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.model.ColumnComparator#setColumn(int)
	 */
	public void setColumn(int column) {
		this.column = column;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.model.ColumnComparator#setDirection(int)
	 */
	public void setDirection(int direction) {
		this.direction = direction;
	}

	/* (non-Javadoc)
	 * @see cn.edu.cuc.yjsy.admin.model.ColumnComparator#reverseDirection()
	 */
	public void reverseDirection() {
		direction = 1 - direction;
	}
}
